package com.globallogic.vkulyk.SSHTool;

import org.junit.*;

import java.io.*;

public class E2E_TargetMode_ExecuteTarget extends SSHToolTester {

	@Before
	public void setup() {
		setVarsFromPropertiesFile("testData/testing.properties");
	}

	@Test
	public void shouldExecuteAllTargetActionsOnAllTargetSockets() {
		//for file put:
		String fileNameForPut = new File(srcFileForPut).getName();
		String dstFileForPut = dstDirForPut + "/" + fileNameForPut;
		new CommandExecutor(host, port, user, password, "rm -f " + dstFileForPut)
				.execute();
		//for file get:
		String fileName = new File(srcFileForGet).getName();
		String dstFile = dstDirForGet + "/" + fileName; 
		new File(dstFile).delete();
		//for all:
		ByteArrayOutputStream outActual = new ByteArrayOutputStream();
		System.setOut(new PrintStream(outActual));
		String[] args = { "-f", targetsFile, "-t", target };
		String outExpected = "LinuxLinuxLinuxLinux"+dstFileForPut;
		
		new CLIHandler().run(args);
		
		//for file put check
		new CommandExecutor(host, port, user, password, "ls " + dstFileForPut)
		.execute();
		//for all except of file get
		String outActualWithRemovedNewLine = outActual.toString().replaceAll(
				"\\n", "");
		Assert.assertEquals("Output not equals", outExpected,
				outActualWithRemovedNewLine);
		//for file get check
		Assert.assertTrue("File for 'get' is not found locally.", new File(dstFile).exists());
		
		System.setOut(null);
	}

}
